package co.za.itweb.itwebafrica;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.RelativeLayout;





@SuppressLint("SetJavaScriptEnabled")
public class DialogActivity extends Activity {

	public ProgressDialog pd;

	private WebView myWebView;
	private String density;
	private int mapwidth;
	private int mapHeight;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		setContentView(R.layout.map_dialog);
		
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		RelativeLayout googleMap = (RelativeLayout) findViewById(R.id.web);
		if(determineDensity()=="ldpi"){
			mapwidth=(int) (width+(width/3));
			mapHeight=(int) (height+(height/3)-180);
		}else if(determineDensity()=="mdpi"){
			mapwidth=width;
			mapHeight=height-150;
		}else if(determineDensity()=="hdpi"){
			mapwidth=(int) width-(width/3);
			mapHeight=(int) (height)-(height/3)-180;
		}else{
			mapwidth=(int) width-(width/2);
			mapHeight=(int) (height)-(height/2)-180;
		}
		
			RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(width,height-180 );
			
			 myWebView = new WebView(getApplicationContext()); 
			
			    
				
				myWebView.getSettings().setJavaScriptEnabled(true);
				myWebView.getSettings().setUseWideViewPort(true);
		    
		

		
		googleMap.addView(myWebView, layoutParams);
		
		String htmlString ="<html>"+ 
				"<head>"+ 
				  "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />"+ 

				  "<script src=\"http://maps.google.com/maps/api/js?sensor=false\""+ 
				          "type=\"text/javascript\"></script>"+
				"</head>"+ 
				"<body>"+
				  "<div id=\"map\" style=\"width:"+mapwidth+"px;height:"+mapHeight+"px;\"\"></div>"+

				  "<script type=\"text/javascript\">"+
				  "var locations = [['Johanesburg', 37.0625, -95.677068," +
				 
				  
				                 "];"+

				   
				   " var map = new google.maps.Map(document.getElementById('map'), {"+
				      "zoom: 4,"+
				      "center: new google.maps.LatLng(0, 0),"+
				      "mapTypeId: google.maps.MapTypeId.ROADMAP"+
				    "});"+
				   
				    "var infowindow = new google.maps.InfoWindow();"+

				    "var marker, i;"+
				    "var bounds = new google.maps.LatLngBounds();"+
				    "for (i = 0; i < locations.length; i++) {"+  
				    "var myLatLng =new google.maps.LatLng(locations[i][1], locations[i][2]);"+
				      "marker = new google.maps.Marker({"+
				        "position: new google.maps.LatLng(locations[i][1], locations[i][2]),"+
				        
				        "map: map"+
				       
				      "});"+
				      
				 "bounds.extend(myLatLng);"+
				 "map.fitBounds(bounds);"+
				     
				      "google.maps.event.addListener(marker, 'click', (function(marker, i) {"+
				        "return function() {"+
				          "infowindow.setContent(locations[i][0]);"+
				          "infowindow.open(map, marker);"+
				        "}"+
				      "})(marker, i));"+
				    "}"+
				  "</script>"+
				"</body>"+
				"</html>";
					
						 

//						myWebView.loadData(htmlString, "text/html", "utf-8");
		
		
		myWebView.loadUrl("http://www.google.com/maps?q=-26.061005,28.060502");
		
		
		 Button buttonclose = (Button) findViewById(R.id.closemap);
			buttonclose.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					finish();
				}
			});
		
	}
	

	public String determineDensity(){
		switch (getResources().getDisplayMetrics().densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			density="ldpi";
			 
		    break;
		case DisplayMetrics.DENSITY_MEDIUM:
			density= "mdpi";
			 
			 break;
		case DisplayMetrics.DENSITY_HIGH:
			density= "hdpi";
			
			 break;
		case DisplayMetrics.DENSITY_XHIGH:
			density= "xdpi";
			
			 break;
		}
		return density;
	}
}
